﻿using System;
using System.Reflection;
using System.Windows;
using BaseConfig;
using IntelligentParkingLot.Base;
using IntelligentParkingLot.Views;
using IntelligentParkingLot.Views.Pages;
using IntelligentParkingLot.Views.Pages.Dialogs;
using IRepositorys;
using IServices;
using Prism.Ioc;
using Prism.Regions;
using Repositorys;
using Services;
using SqlSugar;

namespace IntelligentParkingLot;

/// <summary>
///     Interaction logic for App.xaml
/// </summary>
public partial class App
{
    protected override Window CreateShell()
    {
        return Container.Resolve<MainView>();
    }

    /// <summary>
    ///     配置 Prism 容器
    /// </summary>
    /// <param name="shell"></param>
    protected override void InitializeShell(Window shell)
    {
        base.InitializeShell(shell);

        // 注册导航视图
        Container
            .Resolve<IRegionManager>()
            .RegisterViewWithRegion("ContentRegion", "DashboardView");
    }

    /// <summary>
    ///     注册服务
    /// </summary>
    /// <param name="containerRegistry"></param>
    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        RegisterServices(containerRegistry);
        RegisterViews(containerRegistry);
    }

    /// <summary>
    ///     注册服务
    /// </summary>
    /// <param name="containerRegistry"></param>
    /// <exception cref="NotImplementedException"></exception>
    private void RegisterServices(IContainerRegistry containerRegistry)
    {
        // 注册 SqlSugar
        containerRegistry.RegisterSingleton<ISqlSugarClient>(s =>
        {
            var sqlSugar = new SqlSugarScope(
                new ConnectionConfig
                {
                    DbType = DbType.MySql,
                    ConnectionString = Config.GetDbConnectString(),
                    IsAutoCloseConnection = true,
                    ConfigureExternalServices = new ConfigureExternalServices
                    {
                        //注意:  这儿AOP设置不能少
                        EntityService = (c, p) =>
                        {
                            // 自动设置可空属性
                            if (
                                p.IsPrimarykey == false
                                && new NullabilityInfoContext().Create(c).WriteState
                                    is NullabilityState.Nullable
                            )
                                p.IsNullable = true;
                        }
                    }
                },
                db =>
                {
                    //单例参数配置，所有上下文生效
                    db.Aop.OnLogExecuting = (sql, pars) =>
                    {
                        Console.WriteLine(UtilMethods.GetNativeSql(sql, pars));
                    };
                }
            );
            return sqlSugar;
        });

        // 注册泛型仓储
        containerRegistry.RegisterScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));

        // 注册泛型服务
        containerRegistry.RegisterScoped(typeof(IBaseService<>), typeof(BaseService<>));

        #region 注册具体服务

        containerRegistry.RegisterScoped(typeof(IUserService), typeof(UserService));
        containerRegistry.RegisterScoped(typeof(IMenuService), typeof(MenuService));

        #endregion 注册具体服务
    }

    /// <summary>
    ///     注册视图
    /// </summary>
    /// <param name="containerRegistry"></param>
    private void RegisterViews(IContainerRegistry containerRegistry)
    {
        containerRegistry.RegisterDialogWindow<DialogWindowEx>();
        containerRegistry.RegisterDialog<LoginView>();
        containerRegistry.RegisterDialog<EditMenuDialogView>();

        #region 注册导航视图

        containerRegistry.RegisterForNavigation<DashboardView>();
        containerRegistry.RegisterForNavigation<UserManagementView>();
        containerRegistry.RegisterForNavigation<MenuManagementView>();

        #endregion 注册导航视图
    }
}
